<template><div><p>本文档最新版为 <a href="https://learnku.com/docs/laravel/10.x" target="_blank" rel="noopener noreferrer">10.x</a>，旧版本可能放弃维护，推荐阅读最新版！</p>
<h2 id="laravel-envoy" tabindex="-1"><a class="header-anchor" href="#laravel-envoy"><span>Laravel Envoy</span></a></h2>
<ul>
<li><a href="#introduction">简介</a>
<ul>
<li><a href="#installation">安装</a></li>
</ul>
</li>
<li><a href="#writing-tasks">编写任务</a>
<ul>
<li><a href="#setup">配置</a></li>
<li><a href="#variables">变量</a></li>
<li><a href="#stories">Stories</a></li>
<li><a href="#multiple-servers">多服务器</a></li>
</ul>
</li>
<li><a href="#running-tasks">运行任务</a>
<ul>
<li><a href="#confirming-task-execution">任务确认</a></li>
</ul>
</li>
<li><a href="#notifications">消息通知</a>
<ul>
<li><a href="#slack">Slack</a></li>
<li><a href="#discord">Discord</a></li>
</ul>
</li>
</ul>
<h2 id="简介" tabindex="-1"><a class="header-anchor" href="#简介"><span>简介</span></a></h2>
<p><a href="https://github.com/laravel/envoy" target="_blank" rel="noopener noreferrer">Laravel Envoy</a> 为定义远程服务器的日常任务，提供了一套简洁、轻量的语法。Blade 风格语法即可实现部署任务的配置、Artisan 命令的执行等。目前，Envoy 仅支持 Mac 和 Linux 操作系统。</p>
<h3 id="安装" tabindex="-1"><a class="header-anchor" href="#安装"><span>安装</span></a></h3>
<p>首先，运行 Composer 的 <code v-pre>global require</code> 命令进行全局安装 Envoy:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">composer <span class="token keyword">global</span> <span class="token keyword">require</span> laravel<span class="token operator">/</span>envoy</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>由于全局 Composer 库有时会导致包版本冲突，因此您可能希望考虑使用 <code v-pre>cgr</code> ，它是 <code v-pre>composer global require</code> 命令的直接替代品。 <code v-pre>cgr</code> 库的安装说明可参阅 <a href="https://github.com/consolidation-org/cgr" target="_blank" rel="noopener noreferrer">found on GitHub</a>.</p>
<blockquote>
<p>注意：确保将 <code v-pre>〜/ .composer / vendor / bin</code> 目录放在 PATH 中，以便在终端中运行 <code v-pre>envoy</code> 命令时找到 <code v-pre>envoy</code> 可执行文件。</p>
</blockquote>
<h4 id="更新-envoy" tabindex="-1"><a class="header-anchor" href="#更新-envoy"><span>更新 Envoy</span></a></h4>
<p>您也可以使用 Composer 使您的 Envoy 安装保持最新。 使用 <code v-pre>composer global update</code> 命令将更新所有全局安装的 Composer 包：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">composer <span class="token keyword">global</span> update</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h2 id="编写任务" tabindex="-1"><a class="header-anchor" href="#编写任务"><span>编写任务</span></a></h2>
<p>你的所有 Envoy 任务都应该在项目根目录中的 <code v-pre>Envoy.blade.php</code> 文件中定义。 这是一个让你入门的例子：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">@<span class="token function">servers</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'web'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'user@192.168.1.1'</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line"></span>
<span class="line">@<span class="token function">task</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'on'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'web'</span><span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line">    ls <span class="token operator">-</span>la</span>
<span class="line">@endtask</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如您所见，在文件顶部定义了一个 <code v-pre>@server</code> 数组，允许您在任务声明的 <code v-pre>on</code> 选项中引用这些服务器。 在你的 <code v-pre>@ task</code> 声明中，你应该放置在任务执行时应该在你的服务器上运行的 Bash 代码。</p>
<p>您可以通过将服务器的 IP 地址指定为 <code v-pre>127.0.0.1</code> 来强制脚本在本地运行：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">@<span class="token function">servers</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'localhost'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'127.0.0.1'</span><span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="配置" tabindex="-1"><a class="header-anchor" href="#配置"><span>配置</span></a></h3>
<p>有时，您可能需要在执行 Envoy 任务之前执行一些 PHP 代码。 您可以使用 <code v-pre>@ setup</code> 指令声明变量，并在执行任何其他任务之前执行其他常规 PHP 工作：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">@<span class="token class-name type-declaration">setup</span></span>
<span class="line">    <span class="token variable">$now</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">DateTime</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$environment</span> <span class="token operator">=</span> <span class="token keyword">isset</span><span class="token punctuation">(</span><span class="token variable">$env</span><span class="token punctuation">)</span> <span class="token operator">?</span> <span class="token variable">$env</span> <span class="token punctuation">:</span> <span class="token string double-quoted-string">"testing"</span><span class="token punctuation">;</span></span>
<span class="line">@endsetup</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果在执行任务之前需要其他 PHP 文件，可以在 <code v-pre>Envoy.blade.php</code> 文件的顶部使用 <code v-pre>@include</code> 指令：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">@<span class="token keyword">include</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'vendor/autoload.php'</span><span class="token punctuation">)</span></span>
<span class="line"></span>
<span class="line">@<span class="token function">task</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token comment"># ...</span></span>
<span class="line">@endtask</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="变量" tabindex="-1"><a class="header-anchor" href="#变量"><span>变量</span></a></h3>
<p>如果需要，您可以使用命令行将选项值传递到 Envoy 任务：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">envoy run deploy <span class="token operator">--</span>branch<span class="token operator">=</span>master</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>您可以通过 Blade的「echo」 语法访问任务中的选项。 您也可以在任务中使用 <code v-pre>if</code> 语句和循环。 例如，在执行 <code v-pre>git pull</code> 命令之前，让我们验证 <code v-pre>$branch</code> 变量的存在：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">@<span class="token function">servers</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'web'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'192.168.1.1'</span><span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line"></span>
<span class="line">@<span class="token function">task</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'deploy'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'on'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'web'</span><span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line">    cd site</span>
<span class="line"></span>
<span class="line">    @<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$branch</span><span class="token punctuation">)</span></span>
<span class="line">        git pull origin <span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token variable">$branch</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></span>
<span class="line">    @<span class="token keyword">endif</span></span>
<span class="line"></span>
<span class="line">    php artisan migrate</span>
<span class="line">@endtask</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="stories" tabindex="-1"><a class="header-anchor" href="#stories"><span>Stories</span></a></h3>
<p>Stories 将一组任务分组在一个方便的名称下，允许您将小型，集中的任务分组到大型任务中。 例如， <code v-pre>deploy</code> 故事可以通过在其定义中列出任务名称来运行 <code v-pre>git</code>和 <code v-pre>composer</code> 任务：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">@<span class="token function">servers</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'web'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'192.168.1.1'</span><span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line"></span>
<span class="line">@<span class="token function">story</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'deploy'</span><span class="token punctuation">)</span></span>
<span class="line">    git</span>
<span class="line">    composer</span>
<span class="line">@endstory</span>
<span class="line"></span>
<span class="line">@<span class="token function">task</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'git'</span><span class="token punctuation">)</span></span>
<span class="line">    git pull origin master</span>
<span class="line">@endtask</span>
<span class="line"></span>
<span class="line">@<span class="token function">task</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'composer'</span><span class="token punctuation">)</span></span>
<span class="line">    composer install</span>
<span class="line">@endtask</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>一旦编写了 story ，你可以像一个典型的任务一样运行它：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">envoy run deploy</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="多服务器" tabindex="-1"><a class="header-anchor" href="#多服务器"><span>多服务器</span></a></h3>
<p>Envoy 允许您轻松跨多个服务器运行任务。 首先，在 <code v-pre>@server</code> 声明中添加其他服务器。 应为每个服务器分配一个唯一的名称。 定义了其他服务器后，列出任务的 <code v-pre>on</code> 数组中的每个服务器：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">@<span class="token function">servers</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'web-1'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'192.168.1.1'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'web-2'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'192.168.1.2'</span><span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line"></span>
<span class="line">@<span class="token function">task</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'deploy'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'on'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'web-1'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'web-2'</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line">    cd site</span>
<span class="line">    git pull origin <span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token variable">$branch</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></span>
<span class="line">    php artisan migrate</span>
<span class="line">@endtask</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="并行运行" tabindex="-1"><a class="header-anchor" href="#并行运行"><span>并行运行</span></a></h4>
<p>默认情况下，将在每个服务器上串行执行任务。 换句话说，任务将在继续在第二台服务器上执行之前在第一台服务器上完成运行。 如果您想并行运行多个服务器上的任务，请在任务声明中添加 <code v-pre>parallel</code> 选项：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">@<span class="token function">servers</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'web-1'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'192.168.1.1'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'web-2'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'192.168.1.2'</span><span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line"></span>
<span class="line">@<span class="token function">task</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'deploy'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'on'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'web-1'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'web-2'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'parallel'</span> <span class="token operator">=></span> <span class="token constant boolean">true</span><span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line">    cd site</span>
<span class="line">    git pull origin <span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token variable">$branch</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></span>
<span class="line">    php artisan migrate</span>
<span class="line">@endtask</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="运行任务" tabindex="-1"><a class="header-anchor" href="#运行任务"><span>运行任务</span></a></h2>
<p>要运行在 <code v-pre>Envoy.blade.php</code> 文件中定义的任务或故事，请执行 Envoy 的 <code v-pre>run</code> 命令，传递您要执行的任务或故事的名称。 当任务运行时， Envoy 将运行任务并显示服务器的输出：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">envoy run deploy</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="任务确认" tabindex="-1"><a class="header-anchor" href="#任务确认"><span>任务确认</span></a></h3>
<p>如果您希望在服务器上运行给定任务之前提示您进行确认，则应将 <code v-pre>confirm</code> 指令添加到任务声明中。 此选项对于破坏性操作特别有用：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">@<span class="token function">task</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'deploy'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'on'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'web'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'confirm'</span> <span class="token operator">=></span> <span class="token constant boolean">true</span><span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line">    cd site</span>
<span class="line">    git pull origin <span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token variable">$branch</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></span>
<span class="line">    php artisan migrate</span>
<span class="line">@endtask</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="消息通知" tabindex="-1"><a class="header-anchor" href="#消息通知"><span>消息通知</span></a></h2>
<h3 id="slack" tabindex="-1"><a class="header-anchor" href="#slack"><span>Slack</span></a></h3>
<p>Envoy 还支持在执行每个任务后向 <a href="https://slack.com/" target="_blank" rel="noopener noreferrer">Slack</a> 发送通知。 <code v-pre>@slack</code> 指令接受 Slack 钩子 URL 和通道名称。 您可以通过在 Slack 控制面板中创建 <code v-pre>Incoming WebHooks</code> 集成来检索您的 webhook URL 。 您应该将整个webhook URL传递给 <code v-pre>@slack</code> 指令：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">@finished</span>
<span class="line">    @<span class="token function">slack</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'webhook-url'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'#bots'</span><span class="token punctuation">)</span></span>
<span class="line">@endfinished</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你可以提供以下其中一项作为频道参数：</p>
<ul>
<li>给频道发送通知: <code v-pre>#channel</code></li>
<li>给用户发送通知: <code v-pre>@user</code></li>
</ul>
<h3 id="discord" tabindex="-1"><a class="header-anchor" href="#discord"><span>Discord</span></a></h3>
<p>Envoy 还支持在执行每项任务后向 <a href="https://discord.com/" target="_blank" rel="noopener noreferrer">Discord</a> 发送通知。 <code v-pre>@ discord</code> 指令接受 Discord 钩子 URL 和消息。 您可以通过在 <code v-pre>Webhook</code> 中创建 Webhook 并选择 webhook 应发布到哪个频道来检索您的 webhook URL。 您应该将整个 Webhook URL 传递到<code v-pre>@ discord</code>指令：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">@finished</span>
<span class="line">    @<span class="token function">discord</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'discord-webhook-url'</span><span class="token punctuation">)</span></span>
<span class="line">@endfinished</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>本译文仅用于学习和交流目的，转载请务必注明文章译者、出处、和本文链接<br>
我们的翻译工作遵照 <a href="https://learnku.com/docs/guide/cc4.0/6589" target="_blank" rel="noopener noreferrer">CC 协议</a>，如果我们的工作有侵犯到您的权益，请及时联系我们。</p>
</blockquote>
<hr>
<blockquote>
<p>原文地址：<a href="https://learnku.com/docs/laravel/6.x/envoy/5189" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/6.x/env...</a></p>
<p>译文地址：<a href="https://learnku.com/docs/laravel/6.x/envoy/5189" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/6.x/env...</a></p>
</blockquote>
</div></template>


